<?php
namespace App\Repository\Db;

use App\Repository\Contracts\RoleMenuInterface;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-16 15:00
 *
 * @Description:
 */
class RoleMenuRepository extends Repository implements RoleMenuInterface
{
    protected $rules = [
        'created' => [
            'menu_id' => 'exists:menu,id',
            'role_id' => 'exists:role,id'
        ],
        'updated' => [
            'menu_id' => 'exists:menu,id',
            'role_id' => 'exists:role,id'
        ]
    ];

    protected $messages = [
        'menu_id.required' => "菜单 ID 不存在",
        'role_id.min' => "角色 ID 不存在"
    ];

    protected function modelName()
    {
        return 'App\Model\RoleMenu';
    }

    public function lists($roleId)
    {
        return $this->findAll(['role_id' => $roleId])->toArray();
    }

    public function created(array $data)
    {
        // 表单验证
        $this->validate($data, 'created');

        return $this->save($data);
    }

    public function updated($id, array $data)
    {
        // 表单验证
        $this->validate($data, 'updated');

        return $this->update($data, $id);
    }
}